package com;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * volatile的可见性验证，与不保证原子性验证
 */
class MyDate{
    volatile int a = 0;
    public void setDate(){
        this.a = 60;
    }
    public void add(){
        a++;
    }
    AtomicInteger ato = new AtomicInteger();

    public void atoAdd(){
        ato.getAndIncrement();
    }
}

public class volite {
    public static void main(String[] args) {
        MyDate my = new MyDate();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                for(int j=0;j<2000;j++){
                    my.atoAdd();
                    my.add();
                }
            },String.valueOf(i)).start();
        }
        while(Thread.activeCount()>2){

        }
        System.out.println(my.a);
        System.out.println(my.ato);

    }

    private static void seeOkVolatile() {
        MyDate my = new MyDate();
        new Thread(()->{
            System.out.println(Thread.currentThread().getName() + "开始" );
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            my.add();
            my.setDate();
            System.out.println(Thread.currentThread().getName() + "结束");
        },"aaaa").start();

        while(my.a==0){
        }
        System.out.println(my.a);
        System.out.println(my.ato);
    }
}
